package com.vates.vsp.gui.hotel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.vates.vsp.Principal;
import com.vates.vsp.entidades.Hotel;
import com.vates.vsp.exceptions.HotelNoExistException;
import com.vates.vsp.gui.models.HotelModel;
import com.vates.vsp.gui.models.TipoHabitacionModel;
import com.vates.vsp.servicios.hotel.HotelService;

public class VentanaMostrarTodosHoteles extends JDialog {
	private static final long serialVersionUID = 1L;

	private HotelService hotelSrv;
	
	private final JPanel contentPanel = new JPanel();
	private JTable tableHoteles;
	private JTable tableTH;
	private JPanel panelHoteles;
	private JScrollPane scrollPaneHoteles;

	public VentanaMostrarTodosHoteles() {
		setModal(true);
		setTitle("- Listado de Hoteles -");
		hotelSrv = (HotelService) Principal.context.getBean("hotelSrv");
		setBounds(100, 100, 922, 592);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(Color.WHITE);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setLocationRelativeTo(null);
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			panelHoteles = new JPanel();
			panelHoteles.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Todos los hoteles", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panelHoteles.setBackground(Color.WHITE);
			
			scrollPaneHoteles = new JScrollPane();
			scrollPaneHoteles.setViewportBorder(null);
		}
		
		JPanel panelTipoHab = new JPanel();
		panelTipoHab.setBorder(new TitledBorder(null, "Tipos de habitaci\u00F3n del hotel", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelTipoHab.setBackground(Color.WHITE);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
					.addGap(5)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addComponent(panelTipoHab, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 886, Short.MAX_VALUE)
						.addComponent(panelHoteles, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 886, Short.MAX_VALUE))
					.addGap(5))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(6)
					.addComponent(panelHoteles, GroupLayout.PREFERRED_SIZE, 340, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panelTipoHab, GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		JScrollPane scrollPane = new JScrollPane();
		GroupLayout gl_panelTipoHab = new GroupLayout(panelTipoHab);
		gl_panelTipoHab.setHorizontalGroup(
			gl_panelTipoHab.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelTipoHab.createSequentialGroup()
					.addGap(10)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 854, Short.MAX_VALUE)
					.addGap(10))
		);
		gl_panelTipoHab.setVerticalGroup(
			gl_panelTipoHab.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelTipoHab.createSequentialGroup()
					.addGap(11)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE))
		);
		
		tableTH = new JTable();
		scrollPane.setViewportView(tableTH);
		panelTipoHab.setLayout(gl_panelTipoHab);
		GroupLayout gl_panelHoteles = new GroupLayout(panelHoteles);
		gl_panelHoteles.setHorizontalGroup(
			gl_panelHoteles.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelHoteles.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPaneHoteles, GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panelHoteles.setVerticalGroup(
			gl_panelHoteles.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelHoteles.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPaneHoteles, GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		tableHoteles = new JTable();
		scrollPaneHoteles.setViewportView(tableHoteles);
		tableHoteles.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableHoteles.setBorder(null);
		panelHoteles.setLayout(gl_panelHoteles);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(Color.WHITE);
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton btnAceptar = new JButton("Cerrar");
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				
				JButton btnNuevo = new JButton("Nuevo...");
				btnNuevo.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						nuevoHotel();
					}
				});
				buttonPane.add(btnNuevo);
				
				JButton btnEliminarSeleccin = new JButton("Eliminar selecci\u00F3n");
				btnEliminarSeleccin.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						eliminarSeleccion();
					}
				});
				
				JButton btnBuscar = new JButton("Buscar...");
				btnBuscar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						buscar();
					}
				});
				buttonPane.add(btnBuscar);
				
				JButton btnEditarSeleccion = new JButton("Editar selecci\u00F3n...");
				btnEditarSeleccion.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						editarSeleccion();
					}
				});
				buttonPane.add(btnEditarSeleccion);
				buttonPane.add(btnEliminarSeleccin);
				btnAceptar.setActionCommand("OK");
				buttonPane.add(btnAceptar);
				getRootPane().setDefaultButton(btnAceptar);
			}
		}
		
		actualizarTablaHotel();
		
		tableHoteles.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				actualizarTablaTiposHabitacion();
			}
		});
	}
	
	private void nuevoHotel() {
		JDialog jd = new VentanaHotelNuevo();
		jd.setVisible(true);
		actualizarTablaHotel();
	}
	
	private void actualizarTablaHotel() {
		tableHoteles.setModel(new HotelModel(hotelSrv.buscarTodosHoteles()));
	}
	
	private void actualizarTablaTiposHabitacion() {
		Hotel hotel = obtenerHotelSeleccionado();
		if(hotel != null && hotel.getTipoHabitaciones() != null) {
			tableTH.setModel(new TipoHabitacionModel(hotel.getTipoHabitaciones()));
		}
	}
	
	private void eliminarSeleccion() {
		try {
			if(this.tableHoteles.getSelectedRowCount() != 0) {
				Hotel hotel = obtenerHotelSeleccionado();
				if(JOptionPane.showConfirmDialog(this, "Desea eliminar a " + hotel.getNombre() + "?", "Eliminar Hotel", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0) {
					hotelSrv.bajaHotel(hotel);
					actualizarTablaHotel();
				}
			}
		} catch (HotelNoExistException e) { }
	}

	private Hotel obtenerHotelSeleccionado() {
		try {
			Long id = (Long) tableHoteles.getValueAt(tableHoteles.getSelectedRow(), 0);
			return hotelSrv.buscarHotelPorId(id);
			} catch (HotelNoExistException e) { }
		return null;
	}

	private void buscar() {
		JDialog jd = new VentanaEditarHotel();
		jd.setVisible(true);
		actualizarTablaHotel();
	}
	
	private void editarSeleccion() {
		if(this.tableHoteles.getSelectedRowCount() != 0) {
			JDialog jd = new VentanaEditarHotel(obtenerHotelSeleccionado());
			jd.setVisible(true);
			actualizarTablaHotel();
		}
	}
}
